<?php
/**
 * Created by PhpStorm.
 * User: cong.ding
 * Date: 2017/11/15
 * Time: 16:22
 */


$config = [
  'type' => 'mysql',
  'host' => 'localhost',
  'username' => 'root',
  'password' => 'root',
  'database' => 'stock',
  'port' => '3306',
];

$host = $config['host'];    //主机地址
$username = $config['username'];//用户名
$password = $config['password'];//密码
$database = $config['database'];//数据库
$port = $config['port'];    //端口号

$con = mysqli_connect($host, $username, $password, '', $port);

$result = mysqli_query($con,'show databases');

$data = [];
While ($row = mysqli_fetch_assoc($result)) {
  $data[] = $row['Database'];
}

mysqli_free_result($result);

if (in_array(strtolower($database), $data)) {
  echo "已经安装!<br>如需重新安装，请删除数据库stock!<br>注意备份数据！";
}
else {

  if (mysqli_query($con,"CREATE DATABASE ".$database)) {

    echo "数据库创建完成！<br>表格创建中...<br>";

    mysqli_select_db($con,$database);

    $ar = explode(";", join("", file("stock.sql")));

    foreach ($ar as $v) {
      mysqli_query($con,$v);
    }
    echo "表格创建完成,请返回操作!<br>";
  }
  else {
    echo "创建数据库失败: " . mysqli_error();
  }


}